import $ = require("jquery");
import "jquery.easyui";
import md5 = require("js-md5");

/** 对话框管理器 */
export default class Dialog {
    private readonly option: JQueryEasyUIOptions.dialog;
    private _dialog: JQuery = null;

    /**
     * 初始化 - 对话框管理器
     * @param option 对话框配置项
     */
    constructor(option: JQueryEasyUIOptions.dialog) {
        this.option = option;
        this.MergeOption();
    }

    private MergeOption() {
        var self = this;
        var source_onBeforeOpen = self.option.onBeforeOpen;
        self.option.onBeforeOpen = () => {
            (() => {
                //console.log(`dialog[${self.option.id}].onBeforeOpen() 触发事件方法插入通用执行逻辑`);
                $.messager.progress({
                    noheader: true,
                    title: false,
                    msg: "页面加载中...",
                    interval: 100,
                });
            }).call(this);
            if (source_onBeforeOpen) {
                return source_onBeforeOpen();
            }
            return true;
        };

        var source_onLoad = self.option.onLoad;
        self.option.onLoad = () => {
            if (source_onLoad) {
                source_onLoad();
            }
            (() => {
                $.messager.progress("close");
                self.GetDialog().window("center");
            }).call(this);
        };
    }

    /** 获取实际Jquery对话框对象 */
    public GetDialog(): JQuery {
        var self = this;
        if (self._dialog === null) {
            self._dialog = $("#" + self.option.id);
        }
        return self._dialog;
    }
    private CloseDialog(): void {
        var self = this;
        self._dialog = null;
    }

    /** 初始化创建 */
    public Create(): Dialog {
        var self = this;
        self.option.id = self.option.id ? self.option.id : "IDHTML_Dialog_" + md5(self.option.href);
        if (!self.option.id) {
            $.messager.alert("提示", "对话框ID或跳转地址(href)至少指定一个具体值现在都为空!", "error");
            return self;
        }
        var el = $('<div class="easyui-dialog"></div>');
        el.attr("id", self.option.id);
        if (self.option.style) {
            el.css(self.option.style);
            delete self.option.style;
        }
        var signIdJquery = "#" + self.option.id;
        if ($(signIdJquery).length <= 0) {
            $("body").append(el);
        }
        var dialog = self.GetDialog();
        dialog.dialog(self.option);
        return self;
    }

    /** 打开 */
    public Open(): Dialog {
        var self = this;
        self.GetDialog().dialog("open");
        return self;
    }

    /** 关闭 */
    public Close(): Dialog {
        var self = this;
        self.GetDialog().dialog("close");
        self.CloseDialog();
        return self;
    }

    /** ********************** 调用使用: ********************** **/
}

export class DialogStatic {
    /**
     * 模板 - 正常展示类型对话框
     * @param mconfig 模型配置项
     */
    public static ToGenerate_NormalShowTypeDialog(mconfig: OptionGenerate_NormalShowTypeDialog): Dialog {
        var option: JQueryEasyUIOptions.dialog = {
            title: mconfig.title,
            href: mconfig.url,
            iconCls: "icon-edit",
            style: mconfig.style,
            cache: false,
            closed: true,
            modal: true,
            onLoad: () => {
                mconfig.loadFunc(d);
            },
            onClose: () => {
                if (mconfig.closeFunc) {
                    mconfig.closeFunc(d);
                }
            },
        };
        var d = new Dialog(option);
        return d;
    }

    /**
     * 模板 - 基础编辑保存对话框
     * @param mconfig 模型配置项
     */
    public static ToGenerate_BaiscEditSaveDialog(mconfig: OptionGenerate_BaiscEditSaveDialog): Dialog {
        var buttons: JQueryEasyUIOptions.linkbutton[] = [];
        if (mconfig.isSave) {
            buttons.push({
                iconCls: "icon-ok",
                text: mconfig.saveButtonName || "保存",
                handler: function () {
                    mconfig.saveFunc(d);
                },
            });
        }
        buttons.push({
            iconCls: "icon-cancel",
            text: "取消",
            handler: function () {
                d.Close();
            },
        });
        var option: JQueryEasyUIOptions.dialog = {
            title: mconfig.title,
            href: mconfig.url,
            iconCls: "icon-edit",
            style: mconfig.style,
            cache: false,
            closed: true,
            modal: true,
            onLoad: () => {
                mconfig.loadFunc(d);
            },
            onClose: () => {
                if (mconfig.closeFunc) {
                    mconfig.closeFunc(d);
                }
            },
            buttons: buttons,
        };
        var d = new Dialog(option);
        return d;
    }
}

/** 正常展示类型对话框 */
export interface OptionGenerate_NormalShowTypeDialog {
    /** 标题 */
    title: string;
    /** 编辑页面地址 */
    url: string;
    /** 自定义样式 */
    style?: {};
    /** 加载页面后触发回调 */
    loadFunc: (dialog: Dialog) => void;
    /** 关闭触发回调 (可为空) */
    closeFunc?: (dialog: Dialog) => void;
}
/** 基础编辑保存对话框 */
export interface OptionGenerate_BaiscEditSaveDialog extends OptionGenerate_NormalShowTypeDialog {
    /** 是否出现保存按钮 */
    isSave: boolean;
    /** 保存按钮展示文本 */
    saveButtonName?: string;
    /** 保存触发回调 */
    saveFunc: (dialog: Dialog) => void;
}
